﻿define(['util', 'ajax'], function (util, ajax) {
    "use strict";

    var exports = {},
         ViewModel;

    ViewModel = function (model) {
        var self = this;
        var data = model.detail;
        self.designations = ko.observableArray(model.designations);
        self.titleNames = ko.observableArray(model.nameTitles);

        self.id = ko.observable(data.id);
        self.name = ko.observable(data.name);
        self.titleName = ko.observable(data.nameTitle);
        self.firstName = ko.observable(data.firstName);
        self.lastName = ko.observable(data.lastName);
        self.email = ko.observable(data.email);
        self.phone = ko.observable(data.phone);
        self.mobile = ko.observable(data.mobile);
        self.companyName = ko.observable(data.companyName);
        self.designation = ko.observable(data.designation);
        self.description = ko.observable(data.description);
        self.imageData = ko.observable(data.imageData);

        self.cancel = function() {
            window.location = '/contact/show?id=' + self.id();
        };

        self.submit = function () {
            var designation = _.find(self.designations(), function (val) {
                return val.name == self.designation();
            });

            var titleName = _.find(self.titleNames(), function (val) {
                return val.name == self.titleName();
            });
            
            var form = {
                Id: self.id(),
                Name: self.name(),
                NameTitle: titleName == undefined ? '' : titleName.id,
                FirstName: self.firstName(),
                LastName: self.lastName(),
                Email: self.email(),
                Phone: self.phone(),
                Mobile: self.mobile(),
                CompanyName: self.companyName(),
                Designation: designation == undefined ? '' : designation.id,
                Description: self.description(),
                ImageData: self.tempImage()
            };
            ajax.put('contact', form).done(function (response) {
                if (response.message != '') {
                    window.location = '/contact/show?id=' + self.id();
                }
            })
            ;
        };

        self.tempImage = ko.observable();

        self.tempImage.subscribe(function (newValue) {
            var canvas = document.getElementById("imgCanvas");
            util.image.drawImage(canvas, newValue);
            self.imageData(canvas.toDataURL());
        });

        self.tempImage(util.image.checkOrDefault(data.imageData));

        self.selectPhoto = function () {
            $("#inputFile").click();
        };

        self.capturePhoto = function () {
            $("#inputFile").click();
        };

        $.subscribe('filesDropped', function (files) {

            if (files.length == 0) return;
            var fileToLoad = files[0];
            var fileReader = new FileReader();
            fileReader.onload = function (fileLoadedEvent) {
                self.tempImage(fileLoadedEvent.target.result);
            };
            fileReader.readAsDataURL(fileToLoad);
        });
    };

    exports.exec = function (sectionId, model) {
        var viewModel = new ViewModel(model);
        ko.applyBindings(viewModel, document.getElementById(sectionId));
    };

    return exports;
});